﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.model;
using l1cs.server.serverpackets;
using l1cs.server.datatables;
using l1cs.server.templates;

namespace l1cs.server.clientpackets
{
    public class C_UsePetItem : ClientBasePacket
    {
        private const String _C_USE_PET_ITEM = "[C] C_UsePetItem";
        private static Logger _log = Logger.getLogger( typeof( C_UsePetItem ).FullName );

        public C_UsePetItem( byte[] abyte0, ClientThread clientthread )
            : base( abyte0 )
        {

            int data = readC();
            int petId = readD();
            int listNo = readC();

            L1PetInstance pet = (L1PetInstance)L1World.getInstance().findObject( petId );
            L1PcInstance pc = clientthread.getActiveChar();

            if ( pet == null && pc == null )
            {
                return;
            }
            L1ItemInstance item = (L1ItemInstance)pet.getInventory().getItems()[listNo];
            if ( item == null )
            {
                return;
            }

            if ( item.getItem().getType2() == 0 // 種別：その他のアイテム
                    && item.getItem().getType() == 11 )
            { // petitem
                int itemId = item.getItem().getItemId();
                if ( itemId >= 40749 && itemId <= 40752
                        || itemId >= 40756 && itemId <= 40758 )
                {
                    usePetWeapon( pc, pet, item );
                }
                else if ( itemId >= 40761 && itemId <= 40766 )
                {
                    usePetArmor( pc, pet, item );
                }
                else
                {
                    pc.sendPackets( new S_ServerMessage( 79 ) ); // \f1何も起きませんでした。
                }
            }
            else
            {
                pc.sendPackets( new S_ServerMessage( 79 ) ); // \f1何も起きませんでした。
            }
        }

        private void usePetWeapon( L1PcInstance pc, L1PetInstance pet,
                L1ItemInstance weapon )
        {
            if ( pet.getWeapon() == null )
            {
                setPetWeapon( pc, pet, weapon );
            }
            else
            { // 既に何かを装備している場合、前の装備をはずす
                if ( pet.getWeapon().Equals( weapon ) )
                {
                    removePetWeapon( pc, pet, pet.getWeapon() );
                }
                else
                {
                    removePetWeapon( pc, pet, pet.getWeapon() );
                    setPetWeapon( pc, pet, weapon );
                }
            }
        }

        private void usePetArmor( L1PcInstance pc, L1PetInstance pet,
                L1ItemInstance armor )
        {
            if ( pet.getArmor() == null )
            {
                setPetArmor( pc, pet, armor );
            }
            else
            { // 既に何かを装備している場合、前の装備をはずす
                if ( pet.getArmor().Equals( armor ) )
                {
                    removePetArmor( pc, pet, pet.getArmor() );
                }
                else
                {
                    removePetArmor( pc, pet, pet.getArmor() );
                    setPetArmor( pc, pet, armor );
                }
            }
        }

        private void setPetWeapon( L1PcInstance pc, L1PetInstance pet,
                L1ItemInstance weapon )
        {
            int itemId = weapon.getItem().getItemId();
            L1PetItem petItem = PetItemTable.getInstance().getTemplate( itemId );
            if ( petItem == null )
            {
                return;
            }

            pet.setHitByWeapon( petItem.getHitModifier() );
            pet.setDamageByWeapon( petItem.getDamageModifier() );
            pet.addStr( petItem.getAddStr() );
            pet.addCon( petItem.getAddCon() );
            pet.addDex( petItem.getAddDex() );
            pet.addInt( petItem.getAddInt() );
            pet.addWis( petItem.getAddWis() );
            pet.addMaxHp( petItem.getAddHp() );
            pet.addMaxMp( petItem.getAddMp() );
            pet.addSp( petItem.getAddSp() );
            pet.addMr( petItem.getAddMr() );

            pet.setWeapon( weapon );
            weapon.setEquipped( true );
        }

        private void removePetWeapon( L1PcInstance pc, L1PetInstance pet,
                L1ItemInstance weapon )
        {
            int itemId = weapon.getItem().getItemId();
            L1PetItem petItem = PetItemTable.getInstance().getTemplate( itemId );
            if ( petItem == null )
            {
                return;
            }

            pet.setHitByWeapon( 0 );
            pet.setDamageByWeapon( 0 );
            pet.addStr( -petItem.getAddStr() );
            pet.addCon( -petItem.getAddCon() );
            pet.addDex( -petItem.getAddDex() );
            pet.addInt( -petItem.getAddInt() );
            pet.addWis( -petItem.getAddWis() );
            pet.addMaxHp( -petItem.getAddHp() );
            pet.addMaxMp( -petItem.getAddMp() );
            pet.addSp( -petItem.getAddSp() );
            pet.addMr( -petItem.getAddMr() );

            pet.setWeapon( null );
            weapon.setEquipped( false );
        }

        private void setPetArmor( L1PcInstance pc, L1PetInstance pet,
                L1ItemInstance armor )
        {
            int itemId = armor.getItem().getItemId();
            L1PetItem petItem = PetItemTable.getInstance().getTemplate( itemId );
            if ( petItem == null )
            {
                return;
            }

            pet.addAc( petItem.getAddAc() );
            pet.addStr( petItem.getAddStr() );
            pet.addCon( petItem.getAddCon() );
            pet.addDex( petItem.getAddDex() );
            pet.addInt( petItem.getAddInt() );
            pet.addWis( petItem.getAddWis() );
            pet.addMaxHp( petItem.getAddHp() );
            pet.addMaxMp( petItem.getAddMp() );
            pet.addSp( petItem.getAddSp() );
            pet.addMr( petItem.getAddMr() );

            pet.setArmor( armor );
            armor.setEquipped( true );
        }

        private void removePetArmor( L1PcInstance pc, L1PetInstance pet,
                L1ItemInstance armor )
        {
            int itemId = armor.getItem().getItemId();
            L1PetItem petItem = PetItemTable.getInstance().getTemplate( itemId );
            if ( petItem == null )
            {
                return;
            }

            pet.addAc( -petItem.getAddAc() );
            pet.addStr( -petItem.getAddStr() );
            pet.addCon( -petItem.getAddCon() );
            pet.addDex( -petItem.getAddDex() );
            pet.addInt( -petItem.getAddInt() );
            pet.addWis( -petItem.getAddWis() );
            pet.addMaxHp( -petItem.getAddHp() );
            pet.addMaxMp( -petItem.getAddMp() );
            pet.addSp( -petItem.getAddSp() );
            pet.addMr( -petItem.getAddMr() );

            pet.setArmor( null );
            armor.setEquipped( false );
        }

        override
        public String getType()
        {
            return _C_USE_PET_ITEM;
        }
    }
}